//=============================================================================
// 神魔异世录
//=============================================================================
// 敌人死亡后记录击败数
// 标记为 <EnemyBook Duplicate: x> 的重复敌人（x：该敌人记录到x号敌人的神魔录中）
let _Game_Enemy_die = Game_Enemy.prototype.die;
Game_Enemy.prototype.die = function() {
	_Game_Enemy_die.call(this);
    
    // 变身击杀同步击杀数
    if (this._enemyBeforeTransform){
        // 增加变身齐纳敌人的击杀数
        for(let i = 0; i<this._enemyBeforeTransform.length; i++){
            $gameSystem.incrementDefeatNumber(this.enemyBookId(this._enemyBeforeTransform[i]));
        }
        // 如果记录中不包含当前杀死的敌人，则补充增加其击杀数
        if(!this._enemyBeforeTransform.includes(this.enemyId()))
            $gameSystem.incrementDefeatNumber(this.enemyBookId(this.enemyId()));
    }else{
        $gameSystem.incrementDefeatNumber(this.enemyBookId(this.enemyId()));
    }

    // 增加辨识度
    $gameSystem.incrementIdentify(this.enemyBookId(this.enemyId()));
};

// 敌人中途出现时，记录进神魔异世录
let _Game_Enemy_appear = Game_Enemy.prototype.appear;
Game_Enemy.prototype.appear = function() {
    this.recoverAll();
    _Game_Enemy_appear.call(this);
    $gameSystem.addToEnemyBook(this.enemyBookId(this.enemyId()));
    if($dataEnemies[this.enemyId()].bookComplete)
        $gameSystem.completeEnemyIdentify(this.enemyId());
};

// 敌人变身出现时，记录进神魔异世录
// 敌人变身时：更新其ATB，种族，可收化性
let _Game_Enemy_transform = Game_Enemy.prototype.transform;
Game_Enemy.prototype.transform = function(enemyId) {
    if (!this._enemyBeforeTransform)
        this._enemyBeforeTransform = [];
    if(!this._enemyBeforeTransform.includes(this.enemyId()))
        this._enemyBeforeTransform.push(this.enemyId());

    _Game_Enemy_transform.call(this, enemyId);
    
    $gameSystem.addToEnemyBook(this.enemyBookId(enemyId))
    // 变身敌人一次触发完全解锁。
    if($dataEnemies[enemyId].bookComplete)
        $gameSystem.completeEnemyIdentify(enemyId);
    // 敌人变身时同时改变种族。
    this.changeClass($dataEnemies[enemyId].classId);
    // 敌人变身时同时重置金钱（解决金蚕无法吸收问题）
    this._gold = this.enemy().gold;  //金蚕吸收的金钱，吸收后为0
    this._goldAbsorbed = false;
    // 刷新atb
    SceneManager._scene._atbGaugeWindow ._atbEnemySprite[this.index()].setBattler(this);
    // 更新可收化性
	this.setCanBeCaught(this.enemy().canBeCaught);
};

Game_Enemy.prototype.enemyBookId = function(enemyId) {
    if(this._duplicatedId !== 0){
        return this._duplicatedId;
    }else{
        return enemyId;
    }
};

let _Game_Enemy_setup = Game_Enemy.prototype.setup;
Game_Enemy.prototype.setup = function(enemyId, x, y) {
	const enemy = $dataEnemies[enemyId];
	this._duplicatedId = 0;
	if(enemy)
		this._duplicatedId = enemy.duplicatedId;

    _Game_Enemy_setup.call(this, enemyId, x, y);
};


Game_Enemy.prototype.originalName = function() {
    return this.enemy().name.split("（")[0];
};